package cn.itsource.pet.controller;


import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.UpDownDto;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags={"宠物接口"})
@RestController //@Controller(交给spring管理) + @ResponseBody(方法返回json格式)
@RequestMapping("/pet")
public class PetController {

    /**
     * 注入service层
     */
    @Autowired
    private IPetService petService;

   //this.$http.get("/pet/detail/" + param.id).then(res => {
    @GetMapping("/detail/{id}")
    public AjaxResult detailById(@PathVariable("id") Long id){


        return petService.detailById(id);
    }


  //this.$http.post("/pet/queryHomePage", this.param).then(res => {

    /**
     * 获取上架的分页宠物列表
     *
     * @return
     */
    @PostMapping("/queryHomePage")
    public PageList<Pet> queryHomePage(@RequestBody PetQuery petQuery){


        return petService.queryHomePage(petQuery);
    }


   //宠物上下架
    //      this.post("/pet/changeState",param).then(res=>{
    @PostMapping("/changeState")
    public AjaxResult changeState(@RequestBody UpDownDto upDownDto){


        return petService.changeState(upDownDto);
    }


    /**
     * 查询所有的方法
     */
    @GetMapping
    private AjaxResult findAll(){
        //1.查询所有
        List<Pet> list = petService.findAll();
        //2.封装对象返回
        return AjaxResult.createSuccess(list);
    }

    /**
     * 查询所有的方法
     */
    @PostMapping("/findPage")
    private PageList<Pet> findPage(@RequestBody PetQuery petQuery){

        //调用业务层 alt+enter
        //ctrl + t:快速往后找代码
        return petService.findPage(petQuery);
    }

    /**
     * 查询所有的方法
     *     //this.$http.get("/pet/" + id).then(res => {
     */
    @GetMapping("/{id}")
    private AjaxResult findOne(@PathVariable("id") Long id){

        return AjaxResult.createSuccess(petService.findOne(id));
    }

    /**
     * 新增或者修改  没有id就是新增  没有id就是修改
     * @param pet 宠物对象
     * eclipse风格:alt+shift+z 快速生成try
     * ctrl+shift+enter: 快速生成;
     */
    @PostMapping
    @ApiOperation(value="新增/修改宠物", notes="王天霸")
    public AjaxResult saveOrUpdate(@ApiParam(value = "新增/修改宠物参数", required = true) @RequestBody Pet pet){
        //调用service层
        try {
            petService.saveOrUpdate(pet);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("系统异常,请联系管理员");
        }
    }

    /**
     * 删除方法
     * @param id 主键id
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        //调用删除方法
        try {
            petService.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("系统异常,请联系管理员");
        }
    }


}
